import LTDictionary from "../../LTGame/LTUtils/LTDictionary";
import FGuiData from "./FGuiData";
import BaseUIMediator from "./BaseUIMediator";
export enum EUILayer {
    Main = "Main", // 主界面  永远存在
    Panel = "Panel", // 面板  本层级唯一
    Popup = "Popup", // 弹窗
    Tip = "Tip"
}

export default class FGuiEx {

    public static top: number = 0;
    public static bottom: number = 0;

    public static safeArea: LTGame.SafeArea;

    private static _cacheMap: LTDictionary<string, FGuiData> = new LTDictionary<string, FGuiData>();
    public static panels: BaseUIMediator<any>[] = [];
    public static mainLayer: fgui.GComponent;
    public static panelLayer: fgui.GComponent;
    public static popupLayer: fgui.GComponent;
    public static tipLayer: fgui.GComponent;

    public static: fgui.GObject;

    private static getLayer(layerType: EUILayer): fgui.GComponent {
        switch (layerType) {
            case EUILayer.Main:
                return this.mainLayer;
            case EUILayer.Panel:
                return this.panelLayer;
            case EUILayer.Popup:
                return this.popupLayer;
            case EUILayer.Tip:
                return this.tipLayer;
        }
    }

    public static Init(safeArea: LTGame.SafeArea = null) {
        this.safeArea = safeArea;
        fgui.UIConfig.packageFileExtension = "bin";
        Laya.stage.addChild(fgui.GRoot.inst.displayObject);
        this.mainLayer = fgui.GRoot.inst.addChild(new fgui.GComponent()) as fgui.GComponent;
        this.panelLayer = fgui.GRoot.inst.addChild(new fgui.GComponent()) as fgui.GComponent;
        this.popupLayer = fgui.GRoot.inst.addChild(new fgui.GComponent()) as fgui.GComponent;
        this.tipLayer = fgui.GRoot.inst.addChild(new fgui.GComponent()) as fgui.GComponent;
        this.UpdateAllUI();
        Laya.stage.on(Laya.Event.RESIZE, this, this.UpdateAllUI);
    }

    public static AddUI(uiType, param: FGuiData = null, layer: EUILayer): fgui.GObject {
        let ui = uiType.createInstance() as fgui.GObject;
        if (layer == EUILayer.Panel) {
            let panels = FGuiEx.panels;
            if (panels.length > 0) {
                panels.forEach((panel: BaseUIMediator<any>) => {
                    panel && panel.Hide();
                })
                panels = [];
            }
        }

        this.getLayer(layer).addChild(ui);
        if (param == null) {
            param = new FGuiData();
        }
        if (param == null || !param.needFitScreen) {
            ui.setSize(fgui.GRoot.inst.width, fgui.GRoot.inst.height);
        } else {
            ui.setSize(fgui.GRoot.inst.width, fgui.GRoot.inst.height - this.top - this.bottom);
            ui.y = this.top;
        }
        this._cacheMap.set(ui.constructor.name, param);

        window[ui.constructor.name] = ui;

        console.log("打开界面", ui.constructor.name);

        return ui;
    }

    public static UpdateAllUI() {
        let setWidth = Laya.stage.width;
        let setHeight = Laya.stage.height;
        if (this.safeArea != null) {
            let scale = Laya.stage.width / this.safeArea.width;
            this.top = this.safeArea.top * scale;
            this.bottom = this.safeArea.bottom - this.safeArea.height - this.safeArea.top * scale;
        }
        fgui.GRoot.inst.setSize(setWidth, setHeight);
        let childCount = fgui.GRoot.inst.numChildren;
        for (let i = 0; i < childCount; ++i) {
            let ui = fgui.GRoot.inst.getChildAt(i);
            let getData = this._cacheMap.get(ui.constructor.name);
            if (getData == null || !getData.needFitScreen) {
                ui.setSize(fgui.GRoot.inst.width, fgui.GRoot.inst.height);
            } else {
                ui.setSize(fgui.GRoot.inst.width, fgui.GRoot.inst.height - this.top - this.bottom);
                ui.y = this.top;
            }
        }
    }

    public static CheckIn(ui: fgui.GObject, x: number, y: number): boolean {
        if (x > ui.x && x < ui.x + ui.width && y > ui.y && y < ui.y + ui.height) {
            return true;
        }
        return false;
    }
}